<?php
/**
 * 《节点管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/WifiUtil.class.php');

class Day extends WifiUtil {
	public $tbl = Constant::tbl_wifi_day;
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '已经审核',
		// '3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',

		'date' => '日期',
		'node' => '节点',
		'user_new' => '新增用户数',
		'user_login' => '登录用户数',
		'login' => '登录次数',
		'online' => '最高在线',
		'bytes' => '流量',
		'speed' => '速度',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('t1', strtotime('-1 month'));
		
		$controller->assign('status_id', array(1, 2));

		$controller->display($request, 'wifi/day/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];
		
		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);
		
			return $row;
		}

		if ($px == '1') {
			$row['orderby_x'] = $row['createtime'];
		}

		if ($px == '2') {
			$row['orderby_x'] = $row['modifytime'];
		}
		
		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		$node_s = $request['node_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$my = $this->_my();
		
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		if (!empty($node_s)) {
			$where .= " AND node = '" . $node_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}
		
		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'209', '2'=>'210', '3'=>'211');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'wifi/day/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'wifi/day/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$ip = $request['ip'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, ip, name, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $ip, $name, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/day/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$code = trim($request['code']);
		$name = addslashes($request['name']);
		
		$ip = $request['ip'];
		$ip_vpn = $request['ip_vpn'];
		
		$timeout = $request['timeout'];
		$online = $request['online'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', code = '%s', name = '%s', ip = '%s', 
				ip_vpn = '%s', timeout = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $code, $name, $ip, $ip_vpn, $timeout, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/day/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/day/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row['content_x'] = json_decode($row['content']);
		
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	/**
	 * 分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$node = $this->_view($id);

		$controller->assign_by_ref('node', $node);
	
		require_once(dirname(__FILE__) . '/../emp/Emp.class.php');
		$aa = new Emp();
		
		$emp = $aa->_list();
		
		require_once(dirname(__FILE__) . '/NodeEmp.class.php');
		$bb = new NodeEmp();
		
		$data = array();
		$no = 1;
		while (list($k, $row) = each($emp)) {
			$row['no'] = $no++;
				
			if ($bb->_node_emp($node['name'], $row['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'wifi/day/emp');
		return true;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
	
	function ajax_node($controller, $request) {
		$data = $GLOBALS['HTTP_RAW_POST_DATA'];
		// debug($data);
	
		$aa = json_decode($data, true);
	
		while (list($k, $v) = each($aa)) {
			$this->_ajax_node($v);
		}
	}
	
	function _ajax_node($aa) {
	
		$node = $aa['node'];
		$date = $aa['date'];
	
		$user_new = $aa['user_new'];
		$user_login = $aa['user_login'];
		$login = $aa['login'];
		$online = $aa['online'];
	
		$bytes = $aa['bytes'];
		$period = $aa['period'];
		$speed = $aa['speed'];
		
		if (empty($login)) {
			return;
		}
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE node = '%s' AND date = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node, $date);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET user_new = '%s', user_login = '%s', login = '%s', online = '%s',
					bytes = '%s', period = '%s', speed = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $user_new, $user_login, $login, $online, $bytes, $period, $speed, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(date, node, user_new, user_login, login, online, bytes, period,
					speed, createtime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', NOW())";
			$sql = sprintf($fmt, $this->tbl, $date, $node, $user_new, $user_login, $login, $online, $bytes, $period, $speed);
		}
	
		debug($sql);
		$db->query($sql);
	
		echo "OK";
	}
}
?>
